// 9.23 - kevin
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSignalMapper>
#include <QVector>
#include <QLabel>
#include "toolbar.h"
#include "controls/button.h"
#include "common.h"
#include "controls/circlelabel.h"

ToolBar::ToolBar(QWidget *parent) : QWidget(parent)
{
    initHLayout();
}

void ToolBar::initHLayout()
{
    m_nameList << "电脑体检" << "木马查杀" << "电脑清理" << "系统修复"
               << "优化加速" << "功能大全" << "小金库" << "软件管家";
    QVector<QVector<QString>> pathList = {
        {QVector<QString>() << ":/images/checkUp.png" << ":/images/checkUp2.png" << ":/images/checkUp2.png"},
        {QVector<QString>() << ":/images/trojan.png" << ":/images/trojan1.png" << ":/images/trojan1.png"},
        {QVector<QString>() << ":/images/clear.png" << ":/images/clear1.png" << ":/images/clear1.png"},
        {QVector<QString>() << ":/images/repair.png" << ":/images/repair1.png" << ":/images/repair1.png"},
        {QVector<QString>() << ":/images/speedUp.png" << ":/images/speedUp1.png" << ":/images/speedUp1.png"},
        {QVector<QString>() << ":/images/function.png" << ":/images/function1.png" << ":/images/function1.png"},
        {QVector<QString>() << ":/images/coffers.png" << ":/images/coffers1.png" << ":/images/coffers1.png"},
        {QVector<QString>() << ":/images/steward.png" << ":/images/steward1.png" << ":/images/steward1.png"}
    };

    // 创建布局 -> 水平
    QHBoxLayout* hlayout = new QHBoxLayout(this);
    hlayout->addSpacing(20);
    // 工具栏按钮初始化
    QVector<Button*> buttons;
    for(int i=0; i<m_nameList.size(); ++i)
    {
        buttons.push_back(new Button(pathList.at(i)));
        buttons[i]->setText(m_nameList.at(i));
        hlayout->addWidget(buttons[i]);
    }
    // 弹簧 - expending
    hlayout->addStretch(0);
    // 控件之间的间隔
    hlayout->setSpacing(1);
    // 设置间隙 - 左上右下
    hlayout->setContentsMargins(0, 0, 0, 0);
    // 将布局设置给窗口
    setLayout(hlayout);
    // 根据布局得到的最佳大小设置当前工具栏大小
    this->setFixedHeight(hlayout->sizeHint().height());

    // 设置信号映射
    QSignalMapper* mapper = new QSignalMapper(this);
    for(auto &item : buttons)
    {
        // 创建按钮显示的text 和 按钮地址的映射关系
        m_btnMap.insert(item->text(), item);
        // 设置按钮的信号映射, 每个按钮地址对应一个唯一的字符串
        mapper->setMapping(item, item->text());
        connect(item, SIGNAL(clicked()), mapper, SLOT(map()));
    }
    connect(mapper, SIGNAL(mapped(QString)), this, SLOT(buttonClicked(QString)));

    // 设置默认按钮
    m_curSel = buttons.at(0);
    m_curSel->setSelect();

    // 将用户布局添加到工具栏中
    QLayout* layout = initUserInfo("Kevin.su", 25, ":/images/tx1.jpg");
    hlayout->addLayout(layout);
}

// 用户信息布局初始化
QLayout* ToolBar::initUserInfo(QString user, int level, QString image)
{
    QLabel* pUser = new QLabel(user);
    pUser->setAlignment(Qt::AlignRight);
    QLabel* pLevel = new QLabel("Lv" + QString::number(level));
    pLevel->setFixedWidth(30);
    pLevel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    pLevel->setStyleSheet("background-color: rgb(255, 127, 0); color: white;");
    // 级别自己使用一个水平布局, 使其右对齐
    QHBoxLayout* subHlayout2 = new QHBoxLayout;
    subHlayout2->addStretch(0);
    subHlayout2->addWidget(pLevel);
    subHlayout2->addSpacing(3);
    // 用户名和级别放入垂直布局
    QVBoxLayout* vlayout = new QVBoxLayout;
    vlayout->addSpacing(5);
    vlayout->addWidget(pUser);
    vlayout->addLayout(subHlayout2);
    vlayout->addSpacing(5);
    vlayout->setContentsMargins(0, 0, 15, 0);
    // 垂直布局添加到一个窗口中
    QWidget* subWg = new QWidget;
    subWg->setLayout(vlayout);
    subWg->setFixedSize(100, 50);

    // 头像
    CircleLabel* pImg = new CircleLabel(50, 50);
    pImg->setImage(image);

    // 水平布局 - 放前边的subWg和头像
    QHBoxLayout* hlayout = new QHBoxLayout;
    hlayout->addWidget(subWg);
    hlayout->addWidget(pImg);
    hlayout->addSpacing(20);

    return hlayout;
}

void ToolBar::buttonClicked(QString text)
{
    Common::printLog(text);
    // 刷新按钮的状态
    Button* oldBtn = m_curSel;
    m_curSel = m_btnMap.value(text);
    oldBtn->setNormal();
    m_curSel->setSelect();
    // 根据选中的按钮切换用户界面
//    if(text)
}
